


// 参数x 坐标 有坐标  speed 移动速度

import { onMounted, onUnmounted, ref } from "@vue/runtime-core"
import { game } from "../Game"

//上下同时按，抵消     左右一样
// const commandType = {
//     upAndDown: 'upAndDown',
//     leftAndRight: 'leftAndRight'
// }

export const useKeyboardMove = ({ x, y, speed }) => {
    const moveX = ref(x)
    const moveY = ref(y)

    const commands = []

    const upCommand = {
        type: 'upAndDown',
        dir: -1,
        id: 1
    }
    const downCommand = {
        type: 'upAndDown',
        dir: 1,
        id: 2
    }
    const leftCommand = {
        type: 'leftAndRight',
        dir: -1,
        id: 3
    }
    const rightCommand = {
        type: 'leftAndRight',
        dir: 1,
        id: 4
    }

    const commandMap = {
        ArrowUp: upCommand,
        ArrowDown: downCommand,
        ArrowLeft: leftCommand,
        ArrowRight: rightCommand
    }

    const findUpAndDownCommand = () => {
        return commands.find((command) => {
            return command.type === 'upAndDown'
        })
    }

    const findLeftAndRightCommand = () => {
        return commands.find((command) => {
            return command.type === 'leftAndRight'
        })
    }
    const isExistCommand = (command) => {
        const existCommand = commands.find((c) => {
            return command.id === c.id
        })
        if (existCommand) {
            return true
        } else {
            return false
        }
    }
    const removeCommand = (command) => {
        const index = commands.findIndex((c) => {
            return command.id === c.id
        })
        commands.splice(index, 1)
    }

    const handleKeydown = (e) => { //按住不动是持续触发
        const command = commandMap[e.code]
        if (command && !isExistCommand(command)) {
            commands.unshift(command)
        }
    }

    const handleKeyup = (e) => {
        const command = commandMap[e.code]
        if (command) {
            removeCommand(command)
        }
    }

    const handleTicker = () => {
        const upAndDownCommand = findUpAndDownCommand()
        if (upAndDownCommand) {
            moveY.value += speed*upAndDownCommand.dir
            if (moveY.value >= 900) {
                moveY.value = 900
            }
            if (moveY.value <= 0) {
                moveY.value = 0
            }
        }
        const leftAndRightCommand = findLeftAndRightCommand()
        if (leftAndRightCommand) {
            moveX.value += speed*leftAndRightCommand.dir
            if (moveX.value >= 500) {
                moveX.value = 500
            }
            if (moveX.value <= 0) {
                moveX.value = 0 
            }
        }
    }
    onMounted(() => {
        game.ticker.add(handleTicker)
        window.addEventListener('keydown', handleKeydown)
        window.addEventListener('keyup', handleKeyup)
    })

    onUnmounted(() => {
        game.ticker.remove(handleTicker)
        window.removeEventListener('keydown', handleKeydown)
        window.removeEventListener('keyup', handleKeyup)
    })

    return {
        x: moveX,
        y: moveY
    }
}